package com.jinwei.qunguanjia.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.system.dao.SysNoticeDAO;
import com.jinwei.qunguanjia.system.dto.SysNoticeDTO;
import com.jinwei.qunguanjia.system.po.SysNoticePO;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.ConditionUtils;
import com.jinwei.qunguanjia.common.utils.IdUtils;
import com.jinwei.qunguanjia.common.utils.JsonUtils;
import com.jinwei.qunguanjia.common.utils.SearchUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service("sysNoticeService")
public class SysNoticeServiceImpl implements SysNoticeService {

    @Resource
    private SysNoticeDAO sysNoticeDAO;

    @Override
    public void save(SysNoticeDTO sysNoticeDTO) {
        SysNoticePO sysNoticePO = new SysNoticePO();
        BeanUtils.copyProperties(sysNoticeDTO, sysNoticePO);
        sysNoticePO.setNoticeId(IdUtils.getWorkerId());
        sysNoticeDAO.insert(sysNoticePO);
    }

    @Override
    public void remove(List<String> ids) {
        LambdaQueryWrapper<SysNoticePO> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.in(SysNoticePO::getNoticeId, ids);
        sysNoticeDAO.delete(lambdaWrapper);
    }

    @Override
    public void update(SysNoticeDTO sysNoticeDTO) {
        SysNoticePO sysNoticePO = new SysNoticePO();
        BeanUtils.copyProperties(sysNoticeDTO, sysNoticePO);
        sysNoticeDAO.updateById(sysNoticePO);
    }

    @Override
    public SysNoticeDTO get(String id) {
        SysNoticePO sysNoticePO = sysNoticeDAO.selectById(id);
        SysNoticeDTO sysNoticeDTO = new SysNoticeDTO();
        BeanUtils.copyProperties(sysNoticePO, sysNoticeDTO);
        return sysNoticeDTO;
    }

    @Override
    public SysNoticeDTO get(SysNoticeDTO sysNoticeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sysNoticeDTO);
        // 拼接查询条件
        QueryWrapper<SysNoticePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        SysNoticePO sysNoticePO = sysNoticeDAO.selectOne(queryWrapper);
        if (null == sysNoticePO) {
            return null;
        }
        BeanUtils.copyProperties(sysNoticePO, sysNoticeDTO);
        return sysNoticeDTO;
    }

    @Override
    public List<SysNoticeDTO> list(SysNoticeDTO sysNoticeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sysNoticeDTO);
        // 拼接查询条件
        QueryWrapper<SysNoticePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(sysNoticeDAO.selectList(queryWrapper), SysNoticeDTO.class);
    }

    @Override
    public PageDataInfo<SysNoticeDTO> listPage(SysNoticeDTO sysNoticeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sysNoticeDTO);
        // 拼接查询条件
        QueryWrapper<SysNoticePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<SysNoticePO> list = sysNoticeDAO.selectList(queryWrapper);
        PageInfo<SysNoticePO> pageInfo = new PageInfo<>(list);
        PageDataInfo<SysNoticeDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, SysNoticeDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        return pageDataInfo;
    }
}
